package com.sayice.coursetable.ui.course.week;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sayice.coursetable.logic.Repository;
import com.sayice.coursetable.logic.model.Course;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WeekViewModel extends ViewModel {
    public final List<Course> courses = new ArrayList<>();

    private final MutableLiveData<List<Course>> courseLiveData = new MutableLiveData<>();
    private final MutableLiveData<List<String[]>> dateLiveData = new MutableLiveData<>();

    public LiveData<List<Course>> getCourseLiveData() {
        return courseLiveData;
    }

    public LiveData<List<String[]>> getDateLiveData() {
        return dateLiveData;
    }

    /**
     * 更新课程
     *
     * @param tableId 课表编号
     * @param week    周次
     */
    public void updateCourses(int tableId, int week) {
        Log.d("WeekViewModel", "updateCourses");
        new Thread(() -> {
            Course[] weekCourses = Repository.getWeekCourses(tableId, week);
            List<Course> courses = new ArrayList<>(Arrays.asList(weekCourses));
            courseLiveData.postValue(courses);
        }).start();
    }

    /**
     * 更新日期
     *
     * @param week 周次
     */
    public void updateDates(int week) {
        Log.d("WeekViewModel", "updateDates");
        new Thread(() -> {
            String[][] weekDates = Repository.getWeekDates(week);
            if (weekDates != null) {
                List<String[]> dates = new ArrayList<>(Arrays.asList(weekDates));
                dateLiveData.postValue(dates);
            }
        }).start();
    }
}
